package base;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.mysql.jdbc.Statement;

import bean.Patient;

public class BasePatient {

	public static boolean enregistrer(Base base, Patient patient) {
		boolean res = true;
		String query = "insert into patient";
		query += "(nom,prenom,dateDeNaissance,adresse,codePostal,ville,telephone,poids) values (? , ? , ? , ? , ? , ? , ? , ?)";
		PreparedStatement ps = null;
		try {
			ps = (PreparedStatement)base.getConnection().prepareStatement(query);
			ps.setString(1,patient.getNom());
			ps.setString(2, patient.getPrenom());
			System.out.println(patient.getDateNaissance());
			ps.setString(3, patient.getDateNaissance());
			ps.setString(4, patient.getAdresse());
			ps.setInt(5, patient.getCodePostal());
			ps.setString(6, patient.getVille());
			ps.setInt(7, patient.getNumTel());
			ps.setFloat(8, patient.getPoids());
			
			ps.executeUpdate();
		} catch (SQLException e) {
			System.out.println("BasePatient.enregistrer: erreur "+e.getMessage());
			res = false;
		}
		try {if (ps != null) ps.close();} catch (Exception e) {}
		return res;
	}
	
	public static boolean modifier(Base base, Patient patient) {
		boolean res = true;
		String query = "UPDATE patient SET ";
		query += "nom = ?,prenom = ?,dateDeNaissance = ?,adresse = ?,codePostal = ?,ville = ?,telephone = ?,poids = ? WHERE id = ?";
		PreparedStatement ps = null;
		try {
			ps = (PreparedStatement)base.getConnection().prepareStatement(query);
			ps.setString(1,patient.getNom());
			ps.setString(2, patient.getPrenom());
			System.out.println(patient.getDateNaissance());
			ps.setString(3, patient.getDateNaissance());
			ps.setString(4, patient.getAdresse());
			ps.setInt(5, patient.getCodePostal());
			ps.setString(6, patient.getVille());
			ps.setInt(7, patient.getNumTel());
			ps.setFloat(8, patient.getPoids());
			ps.setInt(9, patient.getId());
			ps.executeUpdate();
		} catch (SQLException e) {
			System.out.println("BasePatient.enregistrer: erreur "+e.getMessage());
			res = false;
		}
		try {if (ps != null) ps.close();} catch (Exception e) {}
		return res;
	}
	
	public static boolean supprimer(Base base, int idPatient) {
		boolean res = true;
		String query = "delete from patient where ";
		query += "id = ?";
		PreparedStatement ps = null;
		try {
			ps = (PreparedStatement)base.getConnection().prepareStatement(query);
			ps.setInt(1,idPatient);
			ps.executeUpdate();
		} catch (SQLException e) {
			System.out.println("BasePatient.supprimer: erreur "+e.getMessage());
			res = false;
		}
		try {if (ps != null) ps.close();} catch (Exception e) {}
		return res;
	}
	
	public static ArrayList<Patient> liste_patient(Base base) {
		ResultSet rs;
		Statement st;
		Patient p;
		ArrayList<Patient> tabPatient = new ArrayList<Patient>();
		try {
			st = (Statement) base.getConnection().createStatement();
			rs = (ResultSet) st.executeQuery("Select * from patient");
			
			while(rs.next()) {
				p = new Patient();
				p.setId(rs.getInt("id"));
				p.setNom(rs.getString("nom"));
				p.setPrenom(rs.getString("prenom"));
				p.setDateNaissance(rs.getString("dateDeNaissance"));
				p.setAdresse(rs.getString("adresse"));
				p.setCodePostal(rs.getInt("codePostal"));
				p.setVille(rs.getString("ville"));
				p.setPoids(rs.getFloat("poids"));
				p.setNumTel(rs.getInt("telephone"));
				tabPatient.add(p);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tabPatient;
	}
	
	public static Patient getPatient(Base base,int idPatient) {
		ResultSet rs;
		Statement st;
		Patient p = new Patient();
		try {
			st = (Statement) base.getConnection().createStatement();
			rs = (ResultSet) st.executeQuery("Select * from patient where id='"+idPatient+"'");
			
			while(rs.next()) {
				p.setId(rs.getInt("id"));
				p.setNom(rs.getString("nom"));
				p.setPrenom(rs.getString("prenom"));
				String dateNaiss = rs.getString("dateDeNaissance");
				String[] aaaaMMdd = dateNaiss.split("-");
				System.out.println(dateNaiss);
				p.setAnnee(Integer.parseInt(aaaaMMdd[0]));
				p.setMois(Integer.parseInt(aaaaMMdd[1]));
				p.setJour(Integer.parseInt(aaaaMMdd[2]));
				p.setDateNaissance(dateNaiss);
				p.setAdresse(rs.getString("adresse"));
				p.setCodePostal(rs.getInt("codePostal"));
				p.setVille(rs.getString("ville"));
				p.setPoids(rs.getFloat("poids"));
				p.setNumTel(rs.getInt("telephone"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return p;
	}
}